//
//  ViewController.m
//  仿写SDWebImage
//
//  Created by 倪啸天 on 16/10/15.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "HMAppInfoModel.h"
#import "HMDownloadIMGOperation.h"
#import "NSOperationManager.h"
//#import "HMImageView.h"

#import "UIImageView+HMDownloadIMG.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet  UIImageView *imageView;

@property (nonatomic, strong) NSArray<HMAppInfoModel *> *modelList;
//@property (nonatomic , strong) NSOperationQueue *queue ;

//@property(nonatomic,strong) NSMutableDictionary *operationCache;

@property (nonatomic, copy) NSString *currentUrlString;


@end

@implementation ViewController

//-(NSMutableDictionary *)operationCache{
//    if(!_operationCache){
//        _operationCache = [NSMutableDictionary dictionary];
//    }
//    return _operationCache;
//}

//- (NSOperationQueue *)queue {
//    if (!_queue) {
//        _queue = [NSOperationQueue new];
//    }
//    return _queue;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self getData];
}

- (void)getData {
    NSString *jsonAddr = @"https://raw.githubusercontent.com/liufan1000/SimpleDemo/master/apps.json";
    
    //获取网络数据,网络上的数据都是二进制的形式 010101010101100
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:jsonAddr
      parameters:nil
        progress:nil
         success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
             //网络访问成功
             //        NSLog(@"%@",[responseObject class]);
             //二进制转换为对象
             NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
             
             NSMutableArray<HMAppInfoModel *> *mArray = [NSMutableArray array];
             //把一个字典数据转换成 模型数组
             [array enumerateObjectsUsingBlock:^(NSDictionary *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
                 HMAppInfoModel *model = [[HMAppInfoModel alloc] init];
                 // KVC
                 [model setValuesForKeysWithDictionary:obj];
                 [mArray addObject:model];
             }];
             
             self.modelList = mArray.copy;
             NSLog(@"ok");

         }
         failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
             //网络访问失败
             NSLog(@"%@", error);
         }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {


    [self displayIMG];
}

- (void)displayIMG {
    
    int num = arc4random_uniform((u_int32_t)self.modelList.count);
    
    HMAppInfoModel *model = self.modelList[num];
    
//    //取消
//    if (![_currentUrlString isEqualToString:model.icon]) {
//        [[NSOperationManager shareManager] cancelOperationWithUrlString:_currentUrlString];
//    }
//  
//  
//    //下载
//    [[NSOperationManager shareManager] downloadIMGWithUrlString:model.icon andCompletedBlock:^(UIImage *image) {
//        self.imageView.image = image;
//    }];
//
//    
//    [self.imageView HM_downloadIMGWithUrlString:model.icon];
    
    
    [self.imageView HM_downloadIMGWithUrlString:model.icon];


    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
